linked-list
Small double linked list.
Install
npm:
npm install linked-list
Use
var LinkedList = require('linked-list')
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
var item3 = new LinkedList.Item()
var list = new LinkedList(item1, item2, item3)
list.head
list.head.next
list.head.next.next
list.head.next.prev
list.tail
list.tail.next
Subclassing:
var inherits = require('inherits')
var List = require('linked-list')
Tokens.prototype.join = join
Token.prototype.toString = toString
inherits(Tokens, List)
inherits(Token, List.Item)
var dogs = new Token('dogs')
var and = new Token('&')
var cats = new Token('cats')
var tokens = new Tokens(dogs, and, cats)
tokens.join(' ')
and.prepend(cats)
and.append(dogs)
tokens.join(' ') + '!'
function Tokens() {
List.apply(this, arguments)
}
function Token(value) {
this.value = value
List.Item.apply(this, arguments)
}
function join(delimiter) {
return this.toArray().join(delimiter)
}
function toString() {
return this.value
}
API
LinkedList([items…])
new LinkedList()
new LinkedList(new LinkedList.Item(), new LinkedList.Item())
Create a new Linked List.
LinkedList.from([items])
LinkedList.from()
LinkedList.from([])
LinkedList.from([new LinkedList.Item(), new LinkedList.Item()])
Create a new this
and adds the given array of items.
Ignores null
or undefined
values.
Throws an error when a given item has no detach
, append
, or prepend
methods.
LinkedList.of([items…])
LinkedList.of()
LinkedList.of(new LinkedList.Item(), new LinkedList.Item())
Creates a new Linked List from the given arguments.
Defers to LinkedList.from
.
LinkedList#append(item)
var list = new LinkedList()
var item = new LinkedList.Item()
list.head === null
item.list === null
list.append(item)
list.head === item
item.list === list
Appends an item to a list.
Throws an error when the given item has no detach
, append
, or prepend
methods.
Returns the given item.
LinkedList#prepend(item)
var list = new LinkedList()
var item = new LinkedList.Item()
list.prepend(item)
Prepends an item to a list.
Throws an error when the given item has no detach
, append
, or prepend
methods.
Returns the given item.
LinkedList#toArray()
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
var list = new LinkedList(item1, item2)
var array = list.toArray()
array[0] === item1
array[1] === item2
array[0].next === item2
array[1].prev === item1
Returns the items in the list in an array.
LinkedList#head
var item = new LinkedList.Item()
var list = new LinkedList(item)
list.head === item
The first item in a list, and null
otherwise.
LinkedList#tail
var list = new LinkedList()
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
list.tail === null
list.append(item1)
list.tail === null
list.append(item2)
list.tail === item2
The last item in a list, and null
otherwise.
Note that a list with only one item has no tail, only a head.
LinkedList#size
var list = new LinkedList()
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
list.size === 0
list.append(item1)
list.size === 1
list.append(item2)
list.size === 2
The number of items in the list.
LinkedList.Item()
var item = new LinkedList.Item()
Creates a new Linked List Item.
LinkedList.Item#append(item)
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
new LinkedList().append(item1)
item1.next === null
item1.append(item2)
item1.next === item2
Adds the given item after the operated on item in a list.
Throws an error when the given item has no detach
, append
, or prepend
methods.
Returns false when the operated on item is not attached to a list, otherwise the
given item.
LinkedList.Item#prepend(item)
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
new LinkedList().append(item1)
item1.prev === null
item1.prepend(item2)
item1.prev === item2
Adds the given item before the operated on item in a list.
Throws an error when the given item has no detach
, append
, or prepend
methods.
Returns false when the operated on item is not attached to a list, otherwise
the given item.
LinkedList.Item#detach()
var item = new LinkedList.Item()
var list = new LinkedList(item)
item.list === list
item.detach()
item.list === null
Removes the operated on item from its parent list.
Removes references to it on its parent list
, and prev
and next
items;
relinking them when possible.
Returns the operated on item.
Even when it was already detached.
LinkedList.Item#next
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
new LinkedList(item1)
item1.next === null
item2.next === null
item1.append(item2)
item1.next === item2
item1.detach()
item1.next === null
The items succeeding item, and null
otherwise.
LinkedList.Item#prev
var item1 = new LinkedList.Item()
var item2 = new LinkedList.Item()
new LinkedList(item)
item1.prev === null
item2.prev === null
item1.append(item2)
item1.prev === item1
item2.detach()
item2.prev === null
The items preceding item, and null
otherwise.
LinkedList.Item#list
var item = new LinkedList.Item()
var list = new LinkedList()
item.list === null
list.append(item)
item.list === list
item.detach()
item.list === null
The items parent list, and null
otherwise.
License
MIT © Titus Wormer